<template>
   <s-form-item :label-col="2">
      <s-button @click="handleReboot" style="margin-right: 16px"
         >重启</s-button
      >
      <s-button @click="handleDefault">恢复出厂设置</s-button>
   </s-form-item>
   <s-modal
      v-model:show="loadShow"
      :title="tip"
      :before-close="closeLoad"
      :show-close="showClose"
   >
      <s-progress />
   </s-modal>
</template>

<script lang="ts" setup>
import { reboot, returnDefault } from "@/request"
import { useLoad } from "@/hooks"
import { SFormItem, Confirm, SButton, SModal, SProgress } from "@/components"
const {
   STATUS,
   tip,
   loadShow,
   closeLoad,
   changeStatus,
   goReboot,
   showClose
} = useLoad()
// 处理重启
const handleReboot = () => {
   changeStatus(STATUS.REBOOT_START)
   reboot()
      .then((res) => {
         if (res) {
            changeStatus(STATUS.REBOOT_ING)
            goReboot()
         } else {
            changeStatus(STATUS.REBOOT_ERROR)
         }
      })
      .catch(() => {
         changeStatus(STATUS.REBOOT_ERROR)
      })
}

// 恢复默认
const handleDefault = () => {
   Confirm("确认恢复出厂设置吗?").then(() => {
      changeStatus(STATUS.DEFAULT_START)
      returnDefault()
         .then((data) => {
            if (data) {
               changeStatus(STATUS.DEFAULT_REBOOTING)
               goReboot(`http://${data.default_ip_addr}`)
            } else {
               changeStatus(STATUS.DEFAULT_ERROR)
            }
         })
         .catch(() => {
            changeStatus(STATUS.DEFAULT_ERROR)
         })
   })
}
</script>

<style scoped lang="less"></style>
